package crazy.memory;

import java.util.HashSet;
import java.util.Set;


public class HashSetTest
{
	public static void main(String[] args)
	{
		Set<Name> s = new HashSet<Name>();
		s.add(new Name("abc", "123"));
		s.add(new Name("abc", "345"));//无法将新的Name对象添加到HashSet中
		s.add(new Name("abc", "3ss"));
		System.out.println(s);
		//System.out.println(s.contains(new Name("abc","123")));//对两个对象进行了比较
	}
}

class Name
{
	private String first;
	private String last;
	
	public Name(String first, String last)
	{
		this.first = first;
		this.last = last;
	}
	
	@Override
	public boolean equals(Object o)
	{
		if (this == o)
		{
			return true;
		}
		if (o.getClass() == this.getClass())
		{
			Name n = (Name) o;
			return n.first.equals(first) ;
		}
		return false;
	}
	
	@Override
	public int hashCode()
	{
		return first.hashCode();
	}
	
	@Override
	public String toString()
	{
		return "Name[first="+first+", last="+last+"]";
	}
	
}
